// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package com.geniusai.aip.auth.service.impl;

import com.geniusai.aip.auth.config.ExternalConfig;
import com.geniusai.aip.auth.domain.UserDO;
import com.geniusai.aip.auth.domain.edsentity.EdsHeaderEntity;
import com.geniusai.aip.auth.domain.edsentity.EdsKeysResponse;
import com.geniusai.aip.auth.domain.edsentity.EdsBucketDTO;
import com.geniusai.aip.auth.domain.edsentity.EdsCreateUserDTO;
import com.geniusai.aip.auth.domain.edsentity.EdsCreateResponse;
import com.geniusai.aip.auth.domain.edsentity.EdsDecryptKeysDTO;
import com.geniusai.aip.auth.domain.edsentity.EdsDecryptKeysResponse;
import com.geniusai.aip.auth.domain.edsentity.EdsDeleteResponse;
import com.geniusai.aip.auth.domain.edsentity.EdsLoginDTO;
import com.geniusai.aip.auth.domain.edsentity.EdsLoginResponse;
import com.geniusai.aip.auth.domain.edsentity.EdsResponse;
import com.geniusai.aip.auth.domain.edsentity.EdsVerifyDTO;
import com.geniusai.aip.auth.domain.edsentity.ObjectUsage;
import com.geniusai.aip.auth.exception.AuthLoginException;
import com.geniusai.aip.auth.exception.AuthLoginExceptionEnum;
import com.geniusai.aip.auth.service.EdsService;
import com.geniusai.aip.feature.common.web.config.RestTemplateHttpsConfig;
import com.geniusai.aip.feature.common.web.utils.UrlUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author wys13822
 * @date 2021/7/13
 * @description
 **/
@Service
@Slf4j
public class EdsServiceImpl implements EdsService {

    @Autowired
    private ExternalConfig externalConfig;

    @Override
    public ObjectUsage getObjectStorage() {
        try {
            EdsHeaderEntity edsHeaderEntity = getEdsHeader();
            HttpEntity<MultiValueMap<String, String>> headerEntity = new HttpEntity<>(edsHeaderEntity.getHeaders());
            RestTemplate restTemplateHttps = new RestTemplate(RestTemplateHttpsConfig.generateHttpRequestFactory());
            ResponseEntity<EdsResponse> edsResponseEntity = restTemplateHttps.exchange(
                    UrlUtils.createHttpsUrl(externalConfig.getEdsServiceUrl(), externalConfig.getEdsServicePort(), EdsConstants.EDS_DASHBOARD), HttpMethod.GET, headerEntity, EdsResponse.class
            );
            if (edsResponseEntity.getBody().getSuccess().equals(EdsConstants.EDS_SUCCESS_CODE)) {
                log.info(String.format("获取homePage成功, 成功信息:%s", edsResponseEntity.toString()));
            } else {
                log.error(String.format("获取homePage失败, 失败信息:%s", edsResponseEntity.toString()));
                throw new AuthLoginException(AuthLoginExceptionEnum.EDS_CREATE_KEYS_FAILED);
            }
            return edsResponseEntity.getBody().getData().getDashboardInfo().getObjectUsage();
        } catch (Exception ex) {
            log.error("获取homePage失败");
            throw new AuthLoginException(AuthLoginExceptionEnum.EDS_CREATE_KEYS_FAILED, ex);
        }
    }

    static class EdsConstants {
        final static String EDS_MODE_SINGAL = "single";
        final static String EDS_MODE_MULTI = "multi";
        final static String EDS_LOGIN_URL = "token";
        final static String EDS_GET_KEYS_URL = "api/objectstore/user_random_keys";
        final static String EDS_VERIFY_URL = "api/setting/account/verify";
        final static String EDS_DECRYPT_KEY_URL = "api/objectstore/decrypt_secret_keys";
        final static String EDS_CREATE_USER_URL = "api/objectstore/users";
        final static String EDS_DASHBOARD = "api/dashboards/homepage";
        final static String EDS_CREATE_BUCKET_URL = "api/objectstore/buckets";
        final static String EDS_DELETE_BUCKET = "api/objectstore/delete_buckets";
        final static String EDS_DELETE_USER = "api/objectstore/delete_users";
        final static String EDS_ACCESS_KEY = "access_key";
        final static String EDS_SECRET_KEY = "secret_key";
        final static Integer EDS_SUCCESS_CODE = 1;
        final static String EDS_SET_COOKIE_FILED = "Set-Cookie";
        final static String EDS_XCSRF_TOKEN = "x-csrf-token";
        final static String EDS_COOKIE_FILED = "Cookie";
    }

    @Override
    public ResponseEntity<EdsLoginResponse> edsLogin(String username, String password) {
        try {
            EdsLoginDTO edsLoginDTO = new EdsLoginDTO();
            edsLoginDTO.setUsername(username);
            edsLoginDTO.setPassword(password);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            HttpEntity<EdsLoginDTO> loginEntity = new HttpEntity<>(edsLoginDTO, headers);
            RestTemplate restTemplateHttps = new RestTemplate(RestTemplateHttpsConfig.generateHttpRequestFactory());
            ResponseEntity<EdsLoginResponse> edsResponseEntity = restTemplateHttps
                    .exchange(UrlUtils.createHttpsUrl(externalConfig.getEdsServiceUrl(), externalConfig.getEdsServicePort(), EdsConstants.EDS_LOGIN_URL), HttpMethod.POST, loginEntity, EdsLoginResponse.class);
            if (edsResponseEntity.getBody().getSuccess().equals(EdsConstants.EDS_SUCCESS_CODE)) {
                log.info(String.format("使用username:%s, password:%s, 登录eds成功, 成功信息:%s", username, password, edsResponseEntity.toString()));
            } else {
                log.error(String.format("使用username:%s, password:%s, 登录eds失败, 失败信息:%s", username, password, edsResponseEntity.toString()));
                throw new AuthLoginException(AuthLoginExceptionEnum.EDS_LOGIN_FAILED);
            }
            return edsResponseEntity;
        } catch (Exception ex) {
            log.error(String.format("使用username:%s, password:%s, 登录eds失败", username, password));
            throw new AuthLoginException(AuthLoginExceptionEnum.EDS_LOGIN_FAILED, ex);
        }
    }

    @Override
    public ResponseEntity<EdsKeysResponse> edsGetKeys(MultiValueMap<String, String> headers) {
        try {
            HttpEntity<MultiValueMap<String, String>> headerEntity = new HttpEntity<>(headers);
            RestTemplate restTemplateHttps = new RestTemplate(RestTemplateHttpsConfig.generateHttpRequestFactory());
            ResponseEntity<EdsKeysResponse> edsResponseEntity = restTemplateHttps.exchange(
                    UrlUtils.createHttpsUrl(externalConfig.getEdsServiceUrl(), externalConfig.getEdsServicePort(), EdsConstants.EDS_GET_KEYS_URL), HttpMethod.GET, headerEntity, EdsKeysResponse.class
            );
            if (edsResponseEntity.getBody().getSuccess().equals(EdsConstants.EDS_SUCCESS_CODE)) {
                log.info(String.format("获取accessKey secretKey成功, 成功信息:%s", edsResponseEntity.toString()));
            } else {
                log.error(String.format("获取accessKey secretKey失败, 失败信息:%s", edsResponseEntity.toString()));
                throw new AuthLoginException(AuthLoginExceptionEnum.EDS_CREATE_KEYS_FAILED);
            }
            return edsResponseEntity;
        } catch (Exception ex) {
            log.error("获取accessKey secretKey失败");
            throw new AuthLoginException(AuthLoginExceptionEnum.EDS_CREATE_KEYS_FAILED, ex);
        }
    }

    @Override
    public ResponseEntity<String> edsVerify(MultiValueMap<String, String> headers) {
        try {
            EdsVerifyDTO edsVerifyDTO = new EdsVerifyDTO();
            edsVerifyDTO.setPassword(externalConfig.getEdsServicePassword());
            edsVerifyDTO.setQueryId(UUID.randomUUID().toString());
            HttpEntity<EdsVerifyDTO> edsVerifyEntity = new HttpEntity<>(edsVerifyDTO, headers);
            RestTemplate restTemplateHttps = new RestTemplate(RestTemplateHttpsConfig.generateHttpRequestFactory());
            ResponseEntity<String> edsVerifyResponse = restTemplateHttps.exchange(
                    UrlUtils.createHttpsUrl(externalConfig.getEdsServiceUrl(), externalConfig.getEdsServicePort(), EdsConstants.EDS_VERIFY_URL), HttpMethod.POST, edsVerifyEntity, String.class
            );
            if (!edsVerifyResponse.getBody().isEmpty()) {
                log.info(String.format("获取Eds验证成功, 成功信息:%s", edsVerifyResponse.toString()));
            } else {
                log.error(String.format("获取Eds验证失败, 失败信息:%s", edsVerifyResponse.toString()));
                throw new AuthLoginException(AuthLoginExceptionEnum.EDS_VERIFY_FAILED);
            }
            return edsVerifyResponse;
        } catch (Exception ex) {
            throw new AuthLoginException(AuthLoginExceptionEnum.EDS_VERIFY_FAILED, ex);
        }
    }

    @Override
    public ResponseEntity<EdsDecryptKeysResponse> edsDecryptKey(ResponseEntity<String> edsVerifyResponse, String token, String cookie, String accessKey, String secreteKey) {
        try {
            List<String> cookieList = edsVerifyResponse.getHeaders().get(EdsConstants.EDS_SET_COOKIE_FILED);
            String ticket = cookieList.get(0).split(";")[0];
            String cookieStr = String.format("%s; %s", ticket, cookie);
            MultiValueMap<String, String> header = new LinkedMultiValueMap<>();
            header.put(EdsConstants.EDS_XCSRF_TOKEN, Collections.singletonList(token));
            header.put(EdsConstants.EDS_COOKIE_FILED, Collections.singletonList(cookieStr));
            EdsDecryptKeysDTO edsDecryptKeysDTO = new EdsDecryptKeysDTO();
            Map<String, String> keyMap = new HashMap<>(2);
            keyMap.put(EdsConstants.EDS_ACCESS_KEY, accessKey);
            keyMap.put(EdsConstants.EDS_SECRET_KEY, secreteKey);
            List<Map<String, String>> listKeys = new ArrayList<>();
            listKeys.add(keyMap);
            edsDecryptKeysDTO.setKeys(listKeys);
            edsDecryptKeysDTO.setQueryId(UUID.randomUUID().toString());
            HttpEntity<EdsDecryptKeysDTO> edsDecryptKeysEntity = new HttpEntity<>(edsDecryptKeysDTO, header);
            RestTemplate restTemplateHttps = new RestTemplate(RestTemplateHttpsConfig.generateHttpRequestFactory());
            ResponseEntity<EdsDecryptKeysResponse> edsDecryptKeysResponse = restTemplateHttps
                    .exchange(UrlUtils.createHttpsUrl(externalConfig.getEdsServiceUrl(), externalConfig.getEdsServicePort(), EdsConstants.EDS_DECRYPT_KEY_URL), HttpMethod.POST, edsDecryptKeysEntity, EdsDecryptKeysResponse.class);
            if (edsDecryptKeysResponse.getBody().getSuccess().equals(EdsConstants.EDS_SUCCESS_CODE)) {
                log.info(String.format("解密Eds secretKey成功, 成功信息:%s", edsDecryptKeysResponse.toString()));
            } else {
                log.error(String.format("解密Eds secretKey失败, 失败信息:%s", edsDecryptKeysResponse.toString()));
                throw new AuthLoginException(AuthLoginExceptionEnum.EDS_VERIFY_FAILED);
            }
            return edsDecryptKeysResponse;
        } catch (Exception ex) {
            throw new AuthLoginException(AuthLoginExceptionEnum.EDS_VERIFY_FAILED, ex);
        }
    }

    @Override
    public void edsCreateUser(EdsCreateUserDTO edsCreateUsersDTO, MultiValueMap<String, String> headers) {
        try {
            HttpEntity<EdsCreateUserDTO> edsCreateUsersEntity = new HttpEntity<>(edsCreateUsersDTO, headers);
            RestTemplate restTemplateHttps = new RestTemplate(RestTemplateHttpsConfig.generateHttpRequestFactory());
            ResponseEntity<EdsCreateResponse> edsCreateUsersResponse = restTemplateHttps.exchange(
                    UrlUtils.createHttpsUrl(externalConfig.getEdsServiceUrl(), externalConfig.getEdsServicePort(), EdsConstants.EDS_CREATE_USER_URL), HttpMethod.POST, edsCreateUsersEntity, EdsCreateResponse.class
            );
            if (edsCreateUsersResponse.getBody().getSuccess().equals(EdsConstants.EDS_SUCCESS_CODE)) {
                log.info(String.format("创建Eds User成功, 成功信息:%s", edsCreateUsersResponse.toString()));
            } else {
                log.error(String.format("创建Eds User失败, 失败信息:%s", edsCreateUsersResponse.toString()));
                throw new AuthLoginException(AuthLoginExceptionEnum.EDS_CREATE_USERS_FAILED);
            }
        } catch (Exception ex) {
            throw new AuthLoginException(AuthLoginExceptionEnum.EDS_CREATE_USERS_FAILED, ex);
        }
    }

    @Override
    public void edsCreateBucket(EdsBucketDTO edsCreateUsersBucketDTO, MultiValueMap<String, String> headers) {
        try {
            HttpEntity<EdsBucketDTO> edsCreateUsersBucketEntity = new HttpEntity<>(edsCreateUsersBucketDTO, headers);
            RestTemplate restTemplateHttps = new RestTemplate(RestTemplateHttpsConfig.generateHttpRequestFactory());
            ResponseEntity<EdsCreateResponse> edsCreateBucketResponse = restTemplateHttps.exchange(
                    UrlUtils.createHttpsUrl(externalConfig.getEdsServiceUrl(), externalConfig.getEdsServicePort(), EdsConstants.EDS_CREATE_BUCKET_URL), HttpMethod.POST, edsCreateUsersBucketEntity, EdsCreateResponse.class
            );
            if (edsCreateBucketResponse.getBody().getSuccess().equals(EdsConstants.EDS_SUCCESS_CODE)) {
                log.info(String.format("创建Eds Bucket成功, 成功信息:%s", edsCreateBucketResponse.toString()));
            } else {
                log.error(String.format("创建Eds Bucket失败, 失败信息:%s", edsCreateBucketResponse.toString()));
                throw new AuthLoginException(AuthLoginExceptionEnum.EDS_CREATE_BUCKETS_FAILED);
            }
        } catch (Exception ex) {
            throw new AuthLoginException(AuthLoginExceptionEnum.EDS_CREATE_BUCKETS_FAILED, ex);
        }
    }

    /**
     * Get Eds Headers
     *
     * @return EdsHeaderEntity
     */
    private EdsHeaderEntity getEdsHeader() {
        EdsHeaderEntity edsHeaderEntity = new EdsHeaderEntity();
        ResponseEntity<EdsLoginResponse> edsLoginResponse = edsLogin(externalConfig.getEdsServiceUserName(), externalConfig.getEdsServicePassword());
        String token = edsLoginResponse.getHeaders().get(EdsConstants.EDS_SET_COOKIE_FILED).get(0).split(";")[0];
        String csrfTokenTmp = edsLoginResponse.getHeaders().get(EdsConstants.EDS_SET_COOKIE_FILED).get(1).split(";")[0];
        String cookieStr = String.format("%s; %s", token, csrfTokenTmp);
        String csrfTokenVal = csrfTokenTmp.split("=")[1];
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.put(EdsConstants.EDS_COOKIE_FILED, Collections.singletonList(cookieStr));
        headers.put(EdsConstants.EDS_XCSRF_TOKEN, Collections.singletonList(csrfTokenVal));
        edsHeaderEntity.setHeaders(headers);
        edsHeaderEntity.setCookieStr(cookieStr);
        edsHeaderEntity.setCsrfTokenVal(csrfTokenVal);
        return edsHeaderEntity;
    }

    @Override
    public void edsDeleteUsers(List<String> deleteBucketsList, List<String> deleteUsersList) {
        try {
            EdsHeaderEntity edsHeaderEntity = getEdsHeader();
            HttpEntity<List<String>> edsDeleteBucketsEntity = new HttpEntity<>(deleteBucketsList, edsHeaderEntity.getHeaders());
            RestTemplate restTemplateHttps = new RestTemplate(RestTemplateHttpsConfig.generateHttpRequestFactory());
            ResponseEntity<EdsDeleteResponse> edsDeleteBucketsResponse = restTemplateHttps.exchange(
                    UrlUtils.createHttpsUrl(externalConfig.getEdsServiceUrl(), externalConfig.getEdsServicePort(), EdsConstants.EDS_DELETE_BUCKET), HttpMethod.POST, edsDeleteBucketsEntity, EdsDeleteResponse.class
            );
            if (edsDeleteBucketsResponse.getBody().getSuccess().equals(EdsConstants.EDS_SUCCESS_CODE)) {
                log.info("删除bucket:{}成功, 成功信息:{}", deleteBucketsList.get(0), edsDeleteBucketsResponse.toString());
            } else {
                log.error("删除bucket:{}失败, 失败信息:{}", deleteBucketsList.get(0), edsDeleteBucketsResponse.toString());
                throw new AuthLoginException(AuthLoginExceptionEnum.EDS_DELETE_BUCKETS_FAILED);
            }
            HttpEntity<List<String>> edsDeleteUsersEntity = new HttpEntity<>(deleteUsersList, edsHeaderEntity.getHeaders());
            ResponseEntity<EdsDeleteResponse> edsDeleteUsersResponse = restTemplateHttps.exchange(
                    UrlUtils.createHttpsUrl(externalConfig.getEdsServiceUrl(), externalConfig.getEdsServicePort(), EdsConstants.EDS_DELETE_USER), HttpMethod.POST, edsDeleteUsersEntity, EdsDeleteResponse.class
            );
            if (edsDeleteUsersResponse.getBody().getSuccess().equals(EdsConstants.EDS_SUCCESS_CODE)) {
                log.info("删除user:{}成功, 成功信息:{}", deleteUsersList.get(0), edsDeleteUsersResponse.toString());
            } else {
                log.error("删除user:{}失败, 失败信息:{}", deleteUsersList.get(0), edsDeleteUsersResponse.toString());
                throw new AuthLoginException(AuthLoginExceptionEnum.EDS_DELETE_USERS_FAILED);
            }
        } catch (Exception ex) {
            throw new AuthLoginException(AuthLoginExceptionEnum.EDS_DELETE_USERS_FAILED, ex);
        }
    }

    @Override
    public void edsDeleteEntrance(List<String> deleteBucketsList, List<String> deleteUsersList) {
        if (EdsConstants.EDS_MODE_SINGAL.equals(externalConfig.getEdsServiceMode())) {
            edsDeleteBucket(deleteBucketsList);
        } else if (EdsConstants.EDS_MODE_MULTI.equals(externalConfig.getEdsServiceMode())) {
            edsDeleteUsers(deleteBucketsList, deleteUsersList);
        } else {
            log.error("执行edsDeleteEntrance时检查到spring.eds-service.mode配置参数错误");
            throw new AuthLoginException(AuthLoginExceptionEnum.EDS_MODE_ERROR_DELETE_FAILED);
        }
    }

    @Override
    public void edsDeleteBucket(List<String> deleteBucketsList) {
        try {
            EdsHeaderEntity edsHeaderEntity = getEdsHeader();
            HttpEntity<List<String>> edsDeleteBucketsEntity = new HttpEntity<>(deleteBucketsList, edsHeaderEntity.getHeaders());
            RestTemplate restTemplateHttps = new RestTemplate(RestTemplateHttpsConfig.generateHttpRequestFactory());
            ResponseEntity<EdsDeleteResponse> edsDeleteBucketsResponse = restTemplateHttps.exchange(
                    UrlUtils.createHttpsUrl(externalConfig.getEdsServiceUrl(), externalConfig.getEdsServicePort(), EdsConstants.EDS_DELETE_BUCKET), HttpMethod.POST, edsDeleteBucketsEntity, EdsDeleteResponse.class
            );
            if (edsDeleteBucketsResponse.getBody().getSuccess().equals(EdsConstants.EDS_SUCCESS_CODE)) {
                log.info("删除bucket:{}成功, 成功信息:{}", deleteBucketsList.get(0), edsDeleteBucketsResponse.toString());
            } else {
                log.error("删除bucket:{}失败, 失败信息:{}", deleteBucketsList.get(0), edsDeleteBucketsResponse.toString());
                throw new AuthLoginException(AuthLoginExceptionEnum.EDS_DELETE_BUCKETS_FAILED);
            }
        } catch (Exception ex) {
            throw new AuthLoginException(AuthLoginExceptionEnum.EDS_DELETE_BUCKETS_FAILED, ex);
        }
    }

    @Override
    public UserDO edsCreateUserBucket(UserDO userDO) {
        EdsHeaderEntity edsHeaderEntity = getEdsHeader();
        MultiValueMap<String, String> headers = edsHeaderEntity.getHeaders();
        ResponseEntity<EdsKeysResponse> edsKeysResponse = edsGetKeys(headers);
        String s3Accesskey = edsKeysResponse.getBody().getData().get(EdsConstants.EDS_ACCESS_KEY);
        String s3EncryptKey = edsKeysResponse.getBody().getData().get(EdsConstants.EDS_SECRET_KEY);
        ResponseEntity<String> edsVerifyEntity = edsVerify(headers);
        ResponseEntity<EdsDecryptKeysResponse> edsDecryptKeysResponse = edsDecryptKey(edsVerifyEntity, edsHeaderEntity.getCsrfTokenVal(), edsHeaderEntity.getCookieStr(), s3Accesskey, s3EncryptKey);
        String s3SecretKey = edsDecryptKeysResponse.getBody().getData().get(0).get(EdsConstants.EDS_SECRET_KEY);
        log.info("当前创建的EDS用户名:{}, s3SecreteKey:{}", userDO.getUsername(), s3SecretKey);
        userDO.setS3AccessKey(s3Accesskey);
        userDO.setS3SecretKey(s3SecretKey);
        userDO.setS3EncryptSecretKey(s3EncryptKey);
        EdsCreateUserDTO edsCreateUserDTO = new EdsCreateUserDTO();
        Map<String, String> keyMap = new HashMap<>(2);
        keyMap.put(EdsConstants.EDS_ACCESS_KEY, userDO.getS3AccessKey());
        keyMap.put(EdsConstants.EDS_SECRET_KEY, userDO.getS3EncryptSecretKey());
        List<Map<String, String>> listKeys = new ArrayList<>();
        listKeys.add(keyMap);
        edsCreateUserDTO.setEmail("");
        edsCreateUserDTO.setKeys(listKeys);
        edsCreateUserDTO.setQueryId(UUID.randomUUID().toString());
        edsCreateUserDTO.setQuota(userDO.getVolumeSize());
        edsCreateUserDTO.setUserName(userDO.getUsername());
        edsCreateUser(edsCreateUserDTO, headers);
        EdsBucketDTO edsBucketDTO = createEdsBucketDTO(userDO);
        edsCreateBucket(edsBucketDTO, headers);
        return userDO;
    }

    private EdsBucketDTO createEdsBucketDTO(UserDO userDO) {
        EdsBucketDTO edsBucketDTO = new EdsBucketDTO();
        Map<String, String> capsMap = new HashMap<>(3);
        capsMap.put("all", "NONE");
        capsMap.put("auth", "NONE");
        capsMap.put("owner", "FULL_CONTROL");
        edsBucketDTO.setCaps(capsMap);
        edsBucketDTO.setName(userDO.getUsername());
        edsBucketDTO.setObjMergeEnable(1);
        edsBucketDTO.setQueryId(UUID.randomUUID().toString());
        edsBucketDTO.setQuota(userDO.getVolumeSize());
        edsBucketDTO.setRedundancyStrategy("ec42_rule");
        edsBucketDTO.setRetrieval("enable");
        if (EdsConstants.EDS_MODE_SINGAL.equals(externalConfig.getEdsServiceMode())) {
            edsBucketDTO.setOwner(externalConfig.getEdsServiceOwner());
        } else if (EdsConstants.EDS_MODE_MULTI.equals(externalConfig.getEdsServiceMode())) {
            edsBucketDTO.setOwner(userDO.getUsername());
        } else {
            log.error("创建edsBucket时检查到spring.eds-service.mode配置参数错误");
            throw new AuthLoginException(AuthLoginExceptionEnum.EDS_MODE_ERROR);
        }
        return edsBucketDTO;
    }

    @Override
    public UserDO edsCreateBucketOnly(UserDO userDO) {
        EdsHeaderEntity edsHeaderEntity = getEdsHeader();
        MultiValueMap<String, String> headers = edsHeaderEntity.getHeaders();
        EdsBucketDTO edsBucketDTO = createEdsBucketDTO(userDO);
        edsCreateBucket(edsBucketDTO, headers);
        userDO.setS3AccessKey(externalConfig.getEdsServiceAccessKey());
        userDO.setS3SecretKey(externalConfig.getEdsServiceSecreteKey());
        userDO.setS3EncryptSecretKey("");
        return userDO;
    }

    @Override
    public UserDO edsCreateEntrance(UserDO userDO) {
        if (EdsConstants.EDS_MODE_SINGAL.equals(externalConfig.getEdsServiceMode())) {
            log.info("开始使用signal模式创建edsBucket,用户:{}", userDO.getUsername());
            return edsCreateBucketOnly(userDO);
        } else if (EdsConstants.EDS_MODE_MULTI.equals(externalConfig.getEdsServiceMode())) {
            log.info("开始使用multi模式创建edsUserBucket,用户:{}", userDO.getUsername());
            return edsCreateUserBucket(userDO);
        } else {
            log.error("执行edsCreateEntrance时检查到spring.eds-service.mode配置参数错误");
            throw new AuthLoginException(AuthLoginExceptionEnum.EDS_MODE_ERROR);
        }
    }

}
